package com.cebbank.group.demoweb.controller;

import com.cebbank.group.demoweb.bean.MyBean;
import org.springframework.boot.context.properties.bind.BindResult;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/validate")
public class ParamNotNullValidateController {

    @RequestMapping
    public void validate(@RequestBody @Validated MyBean myBean, BindingResult bindResult){
        if(bindResult.hasErrors()){
            List<ObjectError> errors = bindResult.getAllErrors();
            for (int i = 0; i < errors.size(); i++) {
                System.out.println(errors.get(i).getDefaultMessage());
            }
        }
    }
}
